package com.psychology.service;

import com.psychology.repository.AppointmentRepository;
import com.psychology.repository.TherapistRepository;
import com.psychology.repository.UserRepository;
import com.psychology.vo.StatsOverviewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StatsService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private TherapistRepository therapistRepository;

    @Autowired
    private AppointmentRepository appointmentRepository;

    public StatsOverviewVO getOverviewStats() {
        StatsOverviewVO stats = new StatsOverviewVO();

        // 获取用户总数（不包括咨询师）
        stats.setUserCount(userRepository.countByUserType("client"));

        // 获取咨询师数量
        stats.setTherapistCount(therapistRepository.countByStatus("active"));

        // 计算满意度
        Double avgRating = appointmentRepository.calculateAverageRating();
        stats.setSatisfactionRate(avgRating != null ? (int) (avgRating * 20) : 95); // 转换为百分比

        // 计算咨询总时长
        Long totalHours = appointmentRepository.calculateTotalConsultationHours();
        stats.setConsultationHours(totalHours != null ? totalHours : 0L);

        return stats;
    }
}